<?php
namespace Common\Model;
use Think\Model;
use Think\Upload;

/**
 * 文章 模型
 */
class RenwuModel extends Model {
	public static $mystat = array('del'=>'-1','forbid'=>'0','allow'=>'1');
	public static $ablemap = array('status'=>'1'); //状态正常的查询条件

	protected $_auto = array(
			array('updatetime',NOW_TIME,self::MODEL_BOTH)
	);
	
	public function myUpdate($data) {
		$id = (int)$data['id'];
		if ($id<=0) {
			$this->error = '请先选择要更新的数据';
			return false;
		}
		//先create验证数据
		if (false ===$this->create($data,self::MODEL_UPDATE)) return false;
	
		//上传门店图片
		if (!empty($data['picurl'])) {
			$setting = C('PICTURE_UPLOAD');
			$Upload = new Upload($setting);
			$image = $Upload->uploadOne($data['picurl']);
			if (!$image) {
				$this->error = $Upload->getError();
				return false;
			}
			$image['path'] = $setting['rootPath'].$image['savepath'].$image['savename']; //在模板里的url路径
			$this->picurl = $image['path'];
		}else {
			unset($this->picurl);
		}
	
		return $this->where('`id`='.$id)->save();
	}
	/**
	 * 新增 文章
	 * @param array $data
	 * @return boolean
	 */
	public function myAddAll($data,$nums) {
		//上传商品图片
		if (!empty($data['picurl'])) {
			$setting = C('PICTURE_UPLOAD');
			$Upload = new Upload($setting);
			$image = $Upload->uploadOne($data['picurl']);
			if (!$image) {
				$this->error = $Upload->getError();
				return false;
			}
			$image['path'] = $setting['rootPath'].$image['savepath'].$image['savename']; //在模板里的url路径
			$data['picurl'] = $image['path'];
		}else {
			unset($data['picurl']);
		}
		
		$this->startTrans();
		for ($i = 0; $i < $nums; $i++) {
			if ($i >= 1){	$data['moban'] = "";	}
			if (false ===$this->create($data,self::MODEL_INSERT)) {
				$this->rollback();
				return false;
			}
			if (false === $this->add()) {
				$this->rollback();
				$this->error = '添加的时候出错了!';
				return false;
			}
		}
		//执行扣费的数据库操作
		$member = new MemberModel();
		if(false === $member->where("id=".MID)->setDec("total_money",$data['kouchu'])){
			$model->rollback();$this->error("扣除总额失败！");
 		}
		if(false === $member->where("id=".MID)->setDec("able_money",$data['kouchu'])){
			$model->rollback();$this->error("扣除可用余额失败！");
		}
		//添加扣费记录
		$jilu = new JiluModel();
			$jldata = array();
 			$jldata['sid'] = MID;
 			$jldata['money'] = $data['kouchu'];
 			$jldata['createtime'] = time();
 			$jldata['cate'] = 3;
 			$jldata['status'] = 1;
 			$jldata['remark'] = "发布任务扣除";
 			$jldata['yue'] = $member->where("id=".MID)->getField("total_money");
	
 		if(false === $jilu->add($jldata)){
 			$model->rollback();$this->error("添加资金记录失败！");
 		}
		
		$this->commit();
		return true;
	}
	/**
	 * 上传审核图片
	 */
	public function myUp($data) {
		//ip图
		if (!empty($data['ipimg'])) {
			$setting = C('PICTURE_UPLOAD');
			$Upload = new Upload($setting);
			$image = $Upload->uploadOne($data['ipimg']);
			if (!$image) {
				$this->error = $Upload->getError();
				return false;
			}
			$image['path'] = $setting['rootPath'].$image['savepath'].$image['savename']; //在模板里的url路径
			$this->ipimg = $image['path'];
		}else {
			unset($this->ipimg);
		}//清除缓存图片
		if (!empty($data['qingchuimg'])) {
			$setting = C('PICTURE_UPLOAD');
			$Upload = new Upload($setting);
			$image = $Upload->uploadOne($data['qingchuimg']);
			if (!$image) {
				$this->error = $Upload->getError();
				return false;
			}
			$image['path'] = $setting['rootPath'].$image['savepath'].$image['savename']; //在模板里的url路径
			$this->qingchuimg = $image['path'];
		}else {
			unset($this->picurl);
		}//购买前截图
		if (!empty($data['goumaiimg'])) {
			$setting = C('PICTURE_UPLOAD');
			$Upload = new Upload($setting);
			$image = $Upload->uploadOne($data['goumaiimg']);
			if (!$image) {
				$this->error = $Upload->getError();
				return false;
			}
			$image['path'] = $setting['rootPath'].$image['savepath'].$image['savename']; //在模板里的url路径
			$this->goumaiimg = $image['path'];
		}else {
			unset($this->picurl);
		}//评论管理图
		if (!empty($data['pinglunimg'])) {
			$setting = C('PICTURE_UPLOAD');
			$Upload = new Upload($setting);
			$image = $Upload->uploadOne($data['pinglunimg']);
			if (!$image) {
				$this->error = $Upload->getError();
				return false;
			}
			$image['path'] = $setting['rootPath'].$image['savepath'].$image['savename']; //在模板里的url路径
			$this->pinglunimg = $image['path'];
		}else {
			unset($this->picurl);
		}
	
		return $this->where('`id`='.$data['id'])->save();
	}
	/*
	 * 取消任务
	 */
	public function renwuQuxiao($id){
		$member = new MemberModel();
		$renwu = new RenwuModel();
		$jilu = new JiluModel();
		$model = new Model();
		$info = $renwu->where("id = ".$id)->find();//任务信息
		$kouchu = $info['yongjin'] + $info['price'];//要返还的金额（扣除一元服务费）
		
		$model->startTrans();
		if (false == $renwu->where("id = ".$id)->setField("status",0)){
			$model->rollback();
			$this->error("操作失败");
		}
		if(false == $member->where("id = ".$info['sid'])->setInc("able_money",$kouchu)){//更改可用余额
			$model->rollback();
			$this->error('更新失败,未知错误!');
		}
	
		if (false == $member->where("id = ".$info['sid'])->setInc("total_money",$kouchu)){//更改总余额
			$model->rollback();
			$this->error('更新失败,未知错误!');
		}
		$data = array();
		$data['money'] = $kouchu;
		$data['remark'] = '商家取消任务扣除金额返还';
		$data['createtime'] = time();
		$data['cate'] = 4;
		$data['status'] = 1;
		$data['sid'] = $info['sid'];
		$data['yue'] = $member->where("id = ".$info['sid'])->getField("total_money");
		if (false == $jilu->add($data)){//添加推荐注册返现记录
			$model->rollback();
			$this->error('更新失败,未知错误!');
		}
		$model->commit();return true;
	}
	/*
	 * 申请放款
	 */
	public function renwuFangkuan($tid,$id){
		$renwu = new RenwuModel();
		$member = new MemberModel();
		$tinfo = $member->where("id = ".$tid)->find();
		$model = new Model();
		$jilu = new JiluModel();
	
		$xian = strval(1 * ((float)$tinfo['bili']/100));
		if(false == $member->where("id = ".$tinfo['id'])->setInc("able_money",$xian)){//更改可用余额
			$model->rollback();
			$this->error('更新失败,未知错误!');
		}
	
		if (false == $member->where("id = ".$tinfo['id'])->setInc("total_money",$xian)){//更改总余额
			$model->rollback();
			$this->error('更新失败,未知错误!');
		}
	
		$tjdata = array();
		$tjdata['money'] = $xian;
		$tjdata['remark'] = '被推荐商家发布任务返现';
		$tjdata['createtime'] = time();
		$tjdata['cate'] = 7;
		$tjdata['status'] = 1;
		$tjdata['sid'] = $tinfo['id'];
		$tjdata['yue'] = $member->where("id = ".$tinfo['id'])->getField("total_money");
		if (false == $jilu->add($tjdata)){//添加推荐注册返现记录
			$model->rollback();
			$this->error('更新失败,未知错误!');
		}
		
		if (false == $renwu->where("id = ".$id)->setField("status",4)){
			$model->rollback();
			$this->error("更新失败，未知错误");
		}
		
		$model->commit();return true;
	}	
}
